package tester;

import java.io.*;
import java.net.*;

import server.Server;
import server.Command;

public class TesterThread extends Thread {
// The Server that spawned us
private Tester tester;
// The Socket connected to our client
private Socket socket;
private boolean isCmd;
//Constructor.
public TesterThread( Tester tester, Socket socket ) {
	// Save the parameters
	this.tester = tester;
	this.socket = socket;
	// Start up the thread
	start();
}

// This runs in a separate thread when start() is called in the
// constructor.
public void run() {
	try {
		// Create a DataInputStream for communication; the client
		// is using a DataOutputStream to write to us
		DataInputStream din = new DataInputStream( socket.getInputStream() );
		// Over and over, forever ...
		while (true) {
			isCmd = false;
			// ... read the next message ...
			char inChar = din.readChar();
			if (inChar != '*')
				tester.log(inChar);
			else
				tester.log("\n");
			
			/*
			// Log the information
			for (TesterCommand cmd : TesterCommand.values()){
				if (message.compareToIgnoreCase(cmd.getCommand()) == 0 ){
					tester.log("command: " + cmd + " from: " + socket);
					isCmd = true;
					break;
				}
			}
			if (!isCmd)
				tester.log( socket + " " + message);
			// ... and have the server send it to all clients
			tester.sendToAll( "Tester: " + message );
		*/}
	} catch( EOFException ie ) {
		//whatever....
	} catch( IOException ie ) {
		ie.printStackTrace();
	} finally {
		tester.removeConnection( socket ); //removes the  connection
	}
}

}